<template>
  <el-menu
    default-active="1-4-1"
    class="el-menu-vertical-demo new-el-menu--sidebar"
    :collapse="isCollapse"
    :collapse-transition="true"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <h3>{{ isCollapse ? '疫情..' : '疫情核酸管理系统' }}</h3>
    <el-menu-item @click="clickMenu(item)" v-for="item in noChildren" :key="item.name" :index="item.name"
                  v-if="!item.isAdmin">
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group v-for="subItem in item.children" :key="subItem.path">
        <el-menu-item @click="clickMenu(subItem)" :index="subItem.path">{{ subItem.label }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
import user from "../api/mockServeData/user";

export default {
  name: "Aside",

  data() {
    return {

      user: {},

      menuData: [
        {
          path: '/main',
          name: 'main',
          label: '首页',
          icon: 's-home',
          url: 'Main/Main'
        },
        {
          path: '/user',
          name: 'user',
          label: '用户管理',
          icon: 'video-play',
          url: 'User/User'
        },
        {
          path: '/edit',
          name: 'articleEdit',
          label: '文章发布',
          icon: 'video-play',
          url: 'Edit/Edit'
        },
        {
          path: '/ncmanage',
          name: 'ncmanage',
          label: '核酸管理',
          icon: 'video-play',
        },
        {
          path: '/cmanage',
          name: 'commManangement',
          label: '社区管理',
          icon: 'video-play',
          isAdmin: !JSON.parse(sessionStorage.getItem("userinfo")).roles.some(item => item.name === 'admin')
        },
        // {
        //   path: '/statistics',
        //   name: 'statistics',
        //   label: '统计',
        //   icon: 'video-play',
        //   // isAdmin: !JSON.parse(sessionStorage.getItem("userinfo")).roles.some(item => item.name === 'admin')
        // },
        {
          label: '文章管理',
          icon: 'location',
          children: [
            {
              path: '/armanage/na_article',
              name: 'na_article',
              label: '国内新闻',
              icon: 'setting',
              url: 'na_article/na_article'
            },
            {
              path: '/armanage/comm_article',
              name: 'comm_article',
              label: '社区新闻',
              icon: 'setting',
              url: 'comm_article/comm_article'
            }
          ]
        },
        {
          label: '其他',
          icon: 'location',
          children: [
            {
              path: '/page1',
              name: 'page1',
              label: '页面1',
              icon: 'setting',
              url: 'Other/PageOne'
            },
            {
              path: '/page2',
              name: 'page2',
              label: '页面2',
              icon: 'setting',
              url: 'Other/PageTwo'
            }
          ]
        }
      ],
    }
  },

  methods: {
    clickMenu(item) {
      if (this.$route.path !== item.path && !(this.$route.path === '/home' && (item.path === '/'))) {
        this.$router.push(item.path)
      }

      //更新tag
      this.$store.commit('selectMenu', item)
    }
  },

  computed: {
    // 没有子菜单
    noChildren() {
      return this.menuData.filter(item => !item.children)
    },
    // 有子菜单
    hasChildren() {
      return this.menuData.filter(item => item.children)
    },
    // menuData() {
    //   // 判断当前数据,如果缓存中没有,当前store中去获取
    //   return JSON.parse(Cookie.get('menu')) || this.$store.state.tab.menu
    // },
    isCollapse() {
      return this.$store.state.isCollapse
    }
  }

}
</script>

<style lang="less" scoped>


.el-menu {
  height: 100vh;
  border: 0;

  h3 {
    color: #ffffff;
    text-align: center;
    line-height: 48px;
    font-weight: 400;
    font-size: 16px;
  }


}

.new-el-menu--sidebar:not(.el-menu--collapse) {
  width: 200px;
}
</style>
